package com.why.controller;

import com.github.pagehelper.PageInfo;
import com.why.pojo.User;
import com.why.pojo.UserVo;
import com.why.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author new wei
 * @date 2022/5/19 22:10
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("list")
    public String list(Model model, UserVo vo, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2") Integer pageSize){
        PageInfo<User> pageInfo=userService.list(pageNum,pageSize,vo);
        model.addAttribute("pg",pageInfo);
        model.addAttribute("vo",vo);
        return "list";
    }
    @RequestMapping("toUpdate")
    public String toUpdate(int id,Model model){
        User user=userService.getUser(id);
        model.addAttribute("user",user);
        return "update";
    }
    @RequestMapping("update")
    public String update(@Validated User user, BindingResult result){
        if (result.hasErrors()){
            return "update";
        }
        userService.update(user);
        return "redirect:list";
    }
    @RequestMapping("del")
    public boolean del(int id){
        int i=userService.del(id);
        if (i>0){
            return true;
        }
        return false;
    }
}
